package pro.view.core;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.*;
import javax.swing.*;

/**
 * @author  sangwo.y
 */
public class Column implements IHolder {
	
	private int id;
	private GameView gv;
	private ArrayList<Card> cards;
	private int offsetX;
	private int offsetY;
	private int cardsCount;
	
	public Column(int id, GameView gv) {
		this.id=id;
		this.gv=gv;
		cardsCount=0;
		offsetX = GameView.COLUMN_X_OFFSET+ 
					id*(GameView.CARD_WIDTH+GameView.COLUMN_X_OFFSET);
		offsetY = GameView.COLUMN_Y_OFFSET;
		cards = new ArrayList<Card>();
	}
	
	public boolean add(Card card) {
		gv.getContentPane().add(card, new Integer(cardsCount));
		card.setHolder(this);
		//card.setText(cardsCount);
		cards.add(card);		
		cardsCount++;
		
		//set card's location
		offsetY=offsetY+GameView.COLUMN_Y_STEP;
		card.setBounds(offsetX, offsetY, GameView.CARD_WIDTH, GameView.CARD_HEIGHT);
		return true;
	}
	
	/**
	 * @return  the id
	 * @uml.property  name="id"
	 */
	public int getId() {
		return id;
	}
	
	
	/**
	 * @return  the cards
	 * @uml.property  name="cards"
	 */
	public List<Card> getCards() {
		return cards;
	}
	public int size() {
		return cards.size();
	}
	
	public boolean contains(Point p) {
		if (	(p.x>=offsetX) &&  (p.y>=offsetY) &&
				(p.x<=offsetX+GameView.CARD_WIDTH) && 
				(p.y<=offsetY+GameView.CARD_HEIGHT+GameView.ACCEPT_SUPL_Y_SPACE)	) {
			return true;
		}
		return false;
	}
	
	
	public void renderContent() {
		offsetY = GameView.COLUMN_Y_OFFSET;
		int count=0;
		for (Card card: cards) {
			((JLayeredPane)gv.getContentPane()).setLayer(card, new Integer(count));
			//card.setText(count);
			offsetY=offsetY+GameView.COLUMN_Y_STEP;
			
			
			//card.setLocation(offsetX, offsetY);
			
			Point p = card.getLocation();
			boolean keepMoving = true;
			int dirX, dirY;
			if (p.x>offsetX) {dirX=-1; } else {dirX=1;}
			if (p.y>offsetY) {dirY=-1; } else {dirY=1;}
			while (keepMoving) {
				if (p.x!=offsetX) p.x+=dirX; 
				if (p.y!=offsetY) p.y+=dirY;
				card.setLocation(p.x,p.y);
				if (	(p.x==offsetX)	&&	(p.y==offsetY)	)
					keepMoving=false;
			}		

			
			
			count++;
		}
	}
	
	
	public List<Card> cardsToDrag(Card card) {
		int src = cards.indexOf(card);
		return cards.subList(src, cardsCount);
	}
	
//	public void mouseDragged(Card card, MouseEvent me) {
//		int src = cards.indexOf(card);
//		//System.out.println(src);
//		if (src!=cardsCount-1) {
//			for (Card c: cards.subList(src, cardsCount)) {
//				c.setLocation(me.getX()-c.offsetX, me.getY()-c.offsetY);
//			}
//		} else {card.setLocation(me.getX()-card.offsetX, me.getY()-card.offsetY); }
//	}
//	
	public List<Card> removeCards(int n) {
		List<Card> out = new ArrayList<Card>();
//		System.out.println("here");
//		System.out.println("n : " + n);
//		System.out.println("cards.size: "+ cards.size());
//		System.out.println("cardsCount : "+cardsCount);
		for (Card theCard: cards.subList(cards.size()-n, cardsCount)) {
			out.add(theCard);
		}
//		System.out.println("not here");
		cardsCount=cardsCount-n;
		
		for (int i=0; i<n; i++) {
			cards.remove(cards.size()-1);
		}
		return out;
		
		//cards.re
	}
	
	public void delete() {
		for (Card card: cards) {
			gv.getContentPane().remove(card);
		}
	}
//	
//	public void addCards(List<Card> cards) {
//		
//	}
//
//	public void mouseReleased() {
//		//moveToPile(id);
//	}

}
